//*****************************************************************************
// JFab (http://code.google.com/p/jfab)
// Copyright (c) 2011-2012 JFab.org
// Admin jfab@jeffreythompson.net
//
// See the file "LICENSE.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//*****************************************************************************

package org.jfab.swingui.decorator;

import javax.swing.Icon;

import org.apache.commons.lang3.StringUtils;
import org.jfab.core.model.CGClass;
import org.jfab.core.model.CGEnum;
import org.jfab.core.model.CGInterface;
import org.jfab.core.model.CGObject;
import org.jfab.core.model.CGSpec;
import org.jfab.swingui.IconManager;
import org.jfab.swingui.editpane.EditPane;

/**
 * Provides a decorator implementation for <code>CGSpec</code>.
 * <p>
 * Possible children are:
 * <ul>
 * <li>interface</li>
 * <li>class</li>
 * <li>enum</li>
 * </ul>
 * </p>
 */
public class CGSpecDecorator extends AbstractDecorator<CGSpec>
{
    /** Child selection values. */
    private static final String[] CHILD_SELECTION_VALUES = { "interface",
            "class", "enum", };

    /**
     * Construct this object with the given parameter.
     * 
     * @param component Component. (required)
     * @param iconManager Icon manager.
     */
    public CGSpecDecorator(final CGSpec component, final IconManager iconManager)
    {
        super(component, null, iconManager);
    }

    @Override
    public void addNode(final String childTypeName)
    {
        if (StringUtils.isNotEmpty(childTypeName))
        {
            final CGSpec parent = getComponent();
            final CGObject newChild = getModelFactory().create(childTypeName);

            if (newChild instanceof CGInterface)
            {
                parent.getInterfaces().add((CGInterface)newChild);
            }
            else if (newChild instanceof CGClass)
            {
                parent.getClasses().add((CGClass)newChild);
            }
            else if (newChild instanceof CGEnum)
            {
                parent.getEnums().add((CGEnum)newChild);
            }
        }
    }

    @Override
    public Object getChild(final int index)
    {
        Object answer;

        final CGSpec cgSpec = getComponent();

        if (index < cgSpec.getInterfaces().size())
        {
            answer = cgSpec.getInterfaces().get(index);
        }
        else if (index < (cgSpec.getInterfaces().size() + cgSpec.getClasses()
                .size()))
        {
            answer = cgSpec.getClasses().get(
                    index - cgSpec.getInterfaces().size());
        }
        else
        {
            answer = cgSpec.getEnums().get(
                    index - cgSpec.getInterfaces().size()
                            - cgSpec.getClasses().size());
        }

        return answer;
    }

    @Override
    public int getChildCount()
    {
        final CGSpec cgSpec = getComponent();

        return cgSpec.getClasses().size() + cgSpec.getEnums().size()
                + cgSpec.getInterfaces().size();
    }

    @Override
    public String[] getChildSelectionValues()
    {
        return CHILD_SELECTION_VALUES;
    }

    @Override
    public String getDisplayName()
    {
        return "CGSpec";
    }

    @Override
    public EditPane<CGSpec> getEditPane()
    {
        return null;
    }

    @Override
    public Icon getIcon()
    {
        return null;
    }

    @Override
    public int getIndexOfChild(final Object child)
    {
        int answer = -1;

        final CGSpec cgSpec = getComponent();

        if (child instanceof CGInterface)
        {
            final int index = cgSpec.getInterfaces().indexOf(child);

            if (index >= 0)
            {
                answer = index;
            }
        }
        else if (child instanceof CGClass)
        {
            final int index = cgSpec.getClasses().indexOf(child);

            if (index >= 0)
            {
                answer = cgSpec.getInterfaces().size() + index;
            }
        }
        else if (child instanceof CGEnum)
        {
            final int index = cgSpec.getEnums().indexOf(child);

            if (index >= 0)
            {
                answer = cgSpec.getInterfaces().size()
                        + cgSpec.getClasses().size() + index;
            }
        }

        return answer;
    }

    @Override
    public boolean isLeaf()
    {
        final CGSpec cgSpec = getComponent();

        return cgSpec.getClasses().isEmpty() && cgSpec.getEnums().isEmpty()
                && cgSpec.getInterfaces().isEmpty();
    }

    @Override
    public boolean removeNode()
    {
        return false;
    }
}
